package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Report;
import com.ruoyi.system.service.IReportService;
import com.ruoyi.thread.TaskAsync;

/**
 * 举报Controller
 * 
 * @author DEREK
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/system/report")
public class ReportController extends BaseController
{
    @Autowired
    private IReportService reportService;
    @Autowired
    private TaskAsync taskAsync;

    /**
     * 查询举报列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(Report report)
    {
        startPage();
        List<Report> list = reportService.selectReportList(report);
        return getDataTable(list);
    }

    /**
     * 导出举报列表
     */
    @PreAuthorize("@ss.hasPermi('system:report:export')")
    @Log(title = "举报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Report report)
    {
        List<Report> list = reportService.selectReportList(report);
        ExcelUtil<Report> util = new ExcelUtil<Report>(Report.class);
        util.exportExcel(response, list, "举报数据");
    }

    /**
     * 获取举报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportService.selectReportById(id));
    }

    /**
     * 新增举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:add')")
    @Log(title = "举报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Report report)
    {
        return toAjax(reportService.insertReport(report));
    }

    /**
     * 修改举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:edit')")
    @Log(title = "举报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Report report)
    {
    	if(null == report.getExamineType()) {
    		return AjaxResult.error("请选择审核类型");
    	}
    	
    	report.setStatus(2);
    	if(1 == report.getExamineType()) {
    		report.setResult("已将该禁用用户");
    		// 推送系统消息
    		taskAsync.sendSystemMsg(report.getReportUserId(), 0l, 13, "", "");
    	}else {
    		report.setResult("审核拒绝,请提供更有利的");
    	}
    	report.setUpdateTime(new Date());
    	report.setAdoptTime(new Date());
    	report.setAdoptUser(getUsername());
    	int updateReport = reportService.updateReport(report);
        return toAjax(updateReport);
    }

    /**
     * 删除举报
     */
    @PreAuthorize("@ss.hasPermi('system:report:remove')")
    @Log(title = "举报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportService.deleteReportByIds(ids));
    }
}
